﻿#ifndef IDICTIONARY_H
#define IDICTIONARY_H

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#ifdef _WINDOWS

#ifdef IDICTIONARY_DLL_EXPORT
#define IDICTIONARY_DLL __declspec(dllexport)
#else
#define IDICTIONARY_DLL __declspec(dllimport)
#endif

#define OSGI_API __cdecl
#include <sys/types.h>
#include "windows.h"

#else

#include <sys/types.h>
#define IDICTIONARY_DLL __attribute__((visibility("default")))
#define __int64 int64_t
#define OSGI_API __attribute__((__cdecl__))
//typedef __attribute__((__cdecl__)) OSGI_API
#define _cdecl __attribute__((__cdecl__))
#define __cdecl __attribute__((__cdecl__))
#endif

#define OSGI_INTERFACE struct
#define OSGI_SUCCESS 0
#define OSGI_ERROR -1
#define NULL 0

extern "C"
{
        IDICTIONARY_DLL void* OSGI_API CreateDictionary();
        IDICTIONARY_DLL void* OSGI_API UnSerializeDictionaryFromBin(
		unsigned char* pData, 
		unsigned int nDataLen);
        IDICTIONARY_DLL void* OSGI_API UnSerializeDictionaryFromXml(
		unsigned char* pData, 
		unsigned int nDataLen);
        IDICTIONARY_DLL void* OSGI_API UnSerializeDictionaryFromJson(
		unsigned char* pData, 
		unsigned int nDataLen);
}

namespace MicroKernelRuntime{

struct IStringEnumer
{
	virtual ~IStringEnumer(){}
        virtual int OSGI_API Count() = 0;
        virtual const char* OSGI_API GetValue(int num) = 0;
        virtual void OSGI_API Destroy() = 0;
};

OSGI_INTERFACE IDICTIONARY_DLL IDictionary
{
	enum DataType
	{
		INT32_TYPE   = 1,
		INT64_TYPE   = 2,
		BOOL_TYPE    = 3,
		DOUBLE_TYPE  = 4,
		STR_TYPE	 = 5,
		MEM_TYPE	 = 6,
		OBJ_TYPE     = 7,
		UNKNOWN_TYPE = 16
	};

        virtual IDictionary* OSGI_API Clone() = 0;

	//消息序列化函数
        virtual int OSGI_API SerializeToBin(
		unsigned char** ppData,
		unsigned int* pnDataLen) = 0;
        virtual int OSGI_API SerializeToXml(
		unsigned char** ppData,
		unsigned int* pnDataLen) = 0;
        virtual int OSGI_API SerializeToJson(
		unsigned char** ppData,
		unsigned int* pnDataLen) = 0;

	//消息组装函数
        virtual int OSGI_API SetMsgType(const char* type) = 0;
        virtual int OSGI_API SetInt32(const char* key, int value) = 0;
        virtual int OSGI_API SetInt64(const char* key, __int64 value) = 0;
        virtual int OSGI_API SetBool(const char* key, bool value) = 0;
        virtual int OSGI_API SetDouble(const char* key, double value) = 0;
        virtual int OSGI_API SetStr(const char* key, const char* value) = 0;
        virtual int OSGI_API SetData(const char* key, unsigned char* pData, unsigned int nDataLen) = 0;
        virtual int OSGI_API SetObj(const char* key, IDictionary* pMsg, bool bCopy) = 0;

    //消息解析函数
        virtual int __cdecl GetMsgType(char** ppType) = 0;
        virtual int __cdecl GetInt32(const char* key, int* pVal) = 0;
        virtual int __cdecl GetInt64(const char* key, __int64* pVal) = 0;
        virtual int __cdecl GetBool(const char* key, bool* pVal) = 0;
        virtual int __cdecl GetDouble(const char* key, double* pVal) = 0;
        virtual int __cdecl GetStr(const char* key, char** ppVal) = 0;
        virtual int __cdecl GetData(const char* key, unsigned char** ppData, unsigned int* pnDataLen) = 0;
        virtual int __cdecl GetObj(const char* key,IDictionary** ppMsg, bool bCopy) = 0;

	//消息元信息解析函数
        virtual int OSGI_API GetAttrCount(int* pCount) = 0;
        virtual int OSGI_API GetKeys(IStringEnumer** ppKeysEnumer) = 0;
        virtual int OSGI_API GetAttrType(const char* key, DataType* pType) = 0;

	//辅助函数
        virtual int OSGI_API RemoveAttr(const char* key) = 0;
        virtual int OSGI_API RemoveAllAttrs() = 0;
        virtual void OSGI_API releaseData(unsigned char* data) = 0;
        virtual void OSGI_API Destroy() = 0;
};

}

#endif	//IDICTIONARY_H
